const server=require('../../utils/server.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    server:'',
    login:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let app=getApp();
    this.setData({server:app.globalData.server})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getPhoneLogin(e){
    if (e.detail.errMsg !== "getPhoneNumber:ok") {
      return;
    }
    let _that=this;
    let ivObj=e.detail.iv;
    let telObj=e.detail.encryptedData;
    
    wx.login({
      fail(res){
        console.log(res);
      },
      success(res){
        let code=res.code;
        wx.getUserInfo({
          success(res){
            var userInfo = res.userInfo;
            var nickName = userInfo.nickName;
            var avatar=userInfo.avatarUrl;
            let userToken= server.getUserToken();
            let header={};
            if(userToken){
              header.TToken=userToken.token;
            }
            wx.request({
              url: server.baseUrl+'/app/wx/login',
              method:'get',
              header:header,
              data:{
                code: code,
                tel:telObj,
                iv:ivObj,
                name:'',
                avatar:avatar
              },
              success:(res)=>{
                console.log(res);
                let data=res.data;
                if(data.code==0){
                  let user=data.data;
                  user.avatarUrl=userInfo.avatarUrl;
                  server.setUserToken(user);
                  _that.setData({hasUserInfo:true});
                  fun(user,shipId);
                }
              }
            })
          }
      })
      }
    })
  }
})